﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using OnlineGameLibrary.Game;
using System.ComponentModel.Composition.Hosting;
using OnlineGameLibrary.Contract;
using System.Reflection;

namespace OnlineGameLibrary.GameEngine.Game
{
    public class GameImporter : IPartImportsSatisfiedNotification
    {
        public GameImporter(string servercomponentsdir)
        {
            var catalog = new DirectoryCatalog(servercomponentsdir);
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }

        public void ComposeParts()
        {
        }

        [ImportMany(AllowRecomposition = true)]
        public Lazy<IGameCreator, IGameCreatorAttributes>[] Games { set; get; }

        public void OnImportsSatisfied()
        {
            //throw new NotImplementedException();
        }

        public GameBase CreateGame(GameRoom gameroom)
        {
            var gamepair = Games.First(lz => lz.Metadata.IdentityName == gameroom.GameConfig.GameIdentityName);
            return gamepair.Value.CreateGame(gameroom);
        }
    }
}
